package com.hpradip.android.documentaryHub;

import java.io.BufferedWriter;
import java.io.StringReader;
import java.net.URL;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import android.text.format.DateUtils;
import android.util.Log;

public class DataProvider {

	public static DocumentaryFeed SelectedFeed;
	public static List<DocumentaryFeed> documentaryFeeds;

	public static List<DocumentaryFeed> getFeed() {
		List<DocumentaryFeed> documentryFeeds = new ArrayList<DocumentaryFeed>();
		for (int i = 0; i < 2; i++) {

			documentryFeeds
					.add(new DocumentaryFeed(
							"Ancient Discoveries",
							"http://cdn.tdfimg.com/wp-content/uploads/2011/07/ancient-discoveries.jpeg",
							"Monday, July 25, 2011 7:00 AM",
							"http://topdocumentaryfilms.com/ancient-discoveries/","history"));
			documentryFeeds
					.add(new DocumentaryFeed(
							"Destination Titan",
							"http://cdn.tdfimg.com/wp-content/uploads/2011/07/destination-titan.jpg",
							"Thursday, July 25, 2011 11:00 AM",
							"http://topdocumentaryfilms.com/destination-titan/","science"));
			documentryFeeds
					.add(new DocumentaryFeed(
							"The Chessboard Killer",
							"http://cdn.tdfimg.com/wp-content/uploads/2011/07/the-chessboard-killer.jpg",
							"Wednesday, July 20, 2011 11:00 AM",
							"http://topdocumentaryfilms.com/the-chessboard-killer/","society"));
		}
		return documentryFeeds;
	}

	public static List<DocumentaryFeed> getFeed(String urlToRssFeed) {
		try {
			URL url = new URL(urlToRssFeed);
			// SAXParserFactory factory = SAXParserFactory.newInstance();
			// SAXParser parser = factory.newSAXParser();
			//
			// XMLReader xmlreader = parser.getXMLReader();
			// RSSFeedHandler theRssHandler = new RSSFeedHandler();
			// xmlreader.setContentHandler(theRssHandler);
			
			InputSource is = new InputSource(url.openStream());
			// xmlreader.parse(is);

			DocumentBuilder documentBuilder = DocumentBuilderFactory
					.newInstance().newDocumentBuilder();
			Document dom = documentBuilder.parse(is);

			Element root = dom.getDocumentElement();
			List<DocumentaryFeed> feeds = new ArrayList<DocumentaryFeed>();
			NodeList items = root.getElementsByTagName("item");
			for (int i = 0; i < items.getLength(); i++) {
				try {
					Element item = (Element) items.item(i);
					DocumentaryFeed feedItem = new DocumentaryFeed();
					NodeList itemContens = item.getChildNodes();
					for (int j = 0; j < itemContens.getLength(); j++) {
						try {
							Element element = (Element) itemContens.item(j);
							String localName = element.getTagName();
							if (localName.equals("title")) {
								feedItem.setTitle(element.getTextContent());
								continue;
							}
							if (localName.equals("description")) {
								feedItem.setRawHTMLData(element.getTextContent());
								try {
									DocumentBuilder documentBuilder2 = DocumentBuilderFactory
											.newInstance().newDocumentBuilder();

									String desc_contents = "<description>"
											+ element.getTextContent().replace(
													"</p><p>", "<p>")
											+ "</description>";
									desc_contents = desc_contents.replace("></a", "/></a");
									Document desc_dom = documentBuilder2
											.parse(new InputSource(
													new StringReader(
															desc_contents)));

									Element desc_root = desc_dom
											.getDocumentElement();

									NodeList img_nodes = desc_root
											.getElementsByTagName("img");
									if (img_nodes.getLength() > 0) {
										NamedNodeMap attributes = img_nodes
												.item(0).getAttributes();
										feedItem.setImageUri(attributes
												.getNamedItem("src")
												.getNodeValue());
									}
									feedItem.setDescription(desc_root
											.getTextContent());
								} catch (Exception ex) {
									Log.d("DataProvider", ex.getMessage());
								}
								continue;
							}
							if (localName.equals("link")) {
								feedItem.setLink(element.getTextContent());
								continue;
							}
							if (localName.equals("category")) {
								if(feedItem.getCategory()==null)
								feedItem.setCategory(element.getTextContent());
								continue;
							}
							if (localName.equals("pubDate")) {
								long date = Date
										.parse(element.getTextContent());
								feedItem.setDate(DateUtils
										.getRelativeTimeSpanString(date)
										.toString());
								continue;

							}
						} catch (ClassCastException ee) {
							continue;
						}
					}
					feeds.add(feedItem);
				} catch (ClassCastException ee) {
					continue;
				}
			}
			documentaryFeeds = feeds;
			return feeds;
			// return theRssHandler.getFeedItems();
		} catch (Exception ee) {
			Log.e("DataProvider", ee.getMessage());
			return new ArrayList<DocumentaryFeed>();
		}
	}
}
